package com.sunday.volunteer.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Address;
import com.sunday.volunteer.entity.Cart;
import com.sunday.volunteer.entity.Product;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Pual's PC on 2016/10/12.
 */
public class ConfirmOrderAdapter extends RecyclerViewAdapter{

    private int type = 0;

    public void setType(int type) {
        this.type = type;
    }
    private SparseArray sparseArray;
    private boolean checkXieyi = false;
    public ConfirmOrderAdapter(Object o, int[] layout) {
        super(o, layout);
        sparseArray = (SparseArray)o;
    }

    @Override
    public int getItemCount() {
        if(sparseArray.keyAt(0) != -1){
            Cart cart = (Cart) sparseArray.get(0);
            if(cart != null && cart.getProducts() != null){
                return 2+cart.getProducts().size();
            }
        }
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(sparseArray.keyAt(0) != -1){
            Cart cart = (Cart) sparseArray.get(0);
            if(cart != null && cart.getProducts() != null){
                if(position == 0){
                    return 0;
                }else if(position == cart.getProducts().size()+1){
                    return 2;
                }else {
                    return 1;
                }
            }
        }
        if(position == 0){
            return 0;
        }else {
            return 2;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(View itemView, int viewType) {
        switch (viewType){
            case 0:
                return new ViewHolderHeader(itemView);
            case 1:
                return new ViewHolderProduct(itemView);
            case 2:
                return new ViewHolderFooter(itemView);
        }
        return null;
    }


    public class ViewHolderHeader extends MyViewHolder {

        @Bind(R.id.to_address)
        View toAddress;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.address)
        TextView address;

        public ViewHolderHeader(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            toAddress.setOnClickListener(onClickListener);
        }

        @Override
        public void bind(MyViewHolder holder, Object object, int position) {
            SparseArray sparseArray = (SparseArray) object;

            if (sparseArray.keyAt(2) != -1 && sparseArray.get(2) != null) {
                Address item = (Address) sparseArray.get(2);
                setText(name, String.format("%s%s%s", item.getReceiveName(), "  ", mobileInvisible(item.getReceiveMobile())));
                setText(address, item.getReceiveAddress());
            } else {
                name.setText("请选择收货地址");
//            }
            }
        }
    }

    private String mobileInvisible(String mobile){
        String text = "";
        if(TextUtils.isEmpty(mobile)){
            text = "****";
        }else {
            if(mobile.length() >= 4){
                text = String.format("%s%s%s" , mobile.substring(0,3) , "****" , mobile.substring(mobile.length() -4 , mobile.length()));
            }else {
                text = "****";
            }
        }
        return text;
    }

    public class ViewHolderProduct extends MyViewHolder{
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.desc)
        TextView desc;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.image_view)
        ImageView imageView;
        public ViewHolderProduct(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }

        @Override
        public void bind(MyViewHolder holder, Object object, int position) {
            SparseArray sparseArray = (SparseArray)object;
            if(sparseArray.keyAt(0) != -1 && sparseArray.get(0) != null){
                Cart cart = (Cart) sparseArray.get(0);
                position = position -1;
                Product item = cart.getProducts().get(position);

                setText(price , item.getPrice());
                setTextFormat(desc , "" , "");
                setText(name , item.getName());
                setTextFormat(num ,"x" , String.valueOf(item.getNumber()));
                setImage(imageView , item.getImage() ,180,180);
            }

        }
    }

    public class ViewHolderFooter extends MyViewHolder {

        public ViewHolderFooter(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void bind(MyViewHolder holder, Object object, int position) {

        }
    }



}
